/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.kopsox.comm.message.ErrorMessage;
import org.kopsox.data.ValidateableValue;
import org.kopsox.data.Value;
import org.kopsox.data.container.ValueContainer;
import org.kopsox.data.impl.Polymorph;
import org.kopsox.util.persistence.impl.data.PersistableContainerBean;
import org.kopsox.util.persistence.impl.data.PersistableValueBean;

/**
 * This utility class provides common methods for creating Persistable-Beans or for loading data from Persistable-Beans
 * 
 * @author Konrad Renner - 18.11.2010
 *
 */
public final class PersistenceUtil {

	private PersistenceUtil() {
		//utility class
	}
	
	/**
	 * Creates an PersistableContainerBean with Data from an ValueContainer
	 * 
	 * @param container - ValueContainer
	 * @return PersistableContainerBean
	 */
	public static final PersistableContainerBean createPersistableContainer(ValueContainer container) {
		
		if(container == null) {
			return null;
		}
		
		PersistableContainerBean bean = new PersistableContainerBean();
		if(container.getDescriptor() != null) {
			bean.setName(container.getDescriptor().getName());
			bean.setNamespace(container.getDescriptor().getNamespace());
		}
		
		for(ValidateableValue<Serializable> value:container.getValues()) {
			bean.add(createPersistableValue(value));
		}
		
		return bean;
	}
	
	/**
	 * Creates an PersistableValueBean with Data from an Value
	 * 
	 * @param value - Value
	 * @return PersistableContainerBean
	 */
	public static final PersistableValueBean createPersistableValue(Value<Serializable> value) {
		
		if(value == null) {
			return null;
		}
		
		PersistableValueBean bean = new PersistableValueBean();
		bean.setName(value.getValueDescriptor().getName());
		bean.setNamespace(value.getValueDescriptor().getNamespace());
		bean.setType(value.getValue()==null?null:value.getValue().getClass());
		bean.setValue(value.getValue());
		
		return bean;
	}
	
	/**
	 * Fills an ValueContainer with Data from an PersistableContainerBean
	 * 
	 * @param container - ValueContainer
	 * @param bean - PersistableContainerBean
	 * @return List<ErrorMessage>
	 */
	public static final List<ErrorMessage> fillValueContainer(PersistableContainerBean bean,ValueContainer container) {
		
		if(bean == null || container == null) {
			throw new IllegalArgumentException("The parameters are not allowed to be null");
		}
		
		List<ErrorMessage> ret = new ArrayList<ErrorMessage>();
		
		for(PersistableValueBean vbean: bean) {
			Polymorph<Serializable> poly = new Polymorph<Serializable>(vbean.getValue());
			
			ret.addAll(container.setValue(vbean.getName(), poly));
		}
		
		return ret;
	}
}
